﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.Linq;

namespace NMemcached.Client.Activities
{
    /// <summary>
    /// Reads multiple items from the cache with detail information
    /// </summary>
    public sealed class GetMultipleWithDetailActivity : MemcachedActivity
    {
        /// <summary>
        /// Gets or sets the keys of the items to read
        /// </summary>
        public InArgument<IEnumerable<string>> Keys { get; set; }

        /// <summary>
        /// Gets or sets a collection of detail entries keyed by cache key
        /// </summary>
        public OutArgument<IDictionary<string, CacheDetail>> Result { get; set; }

        /// <summary>
        /// Initializes a new instance of the GetMultipleWithDetailActivity class
        /// </summary>
        public GetMultipleWithDetailActivity()
            : base()
        {
        }

        /// <summary>
        /// Performs the execution of the activity
        /// </summary>
        /// <param name="context">The execution context under which the activity executes</param>
        protected override void Execute(CodeActivityContext context)
        {
            base.Execute(context);
            IEnumerable<string> keys = this.Keys.Get(context);
            if (keys == null)
            {
                throw new ArgumentNullException("Keys");
            }
            else
            {
                this.Result.Set(context, base.CacheConnection.GetWithDetail(keys.ToArray()));
            }
        }
    }
}
